package com.fbms.common.result;

import lombok.Getter;

import java.util.Collection;

import static com.fbms.common.result.ResFlag.*;



/**
 * @author lan
 * @since 2023/03/15  10:21
 * list结果集构建
 */
@Getter
public class MultiResBuilder<T> {


    /**
     * 通过数据直接构造一个结果出来。
     *
     * @param t   数据
     * @param <T> 类型
     * @return 结果
     */
    public static <T> MultiResponse<T> of(Collection<T> t) {
        return MultiResBuilder.<T>successBuilder()
                .data(t)
                .build();
    }

    /**
     * 错误结果集的 快速构建
     *
     * @param msg 消息
     * @param <T> 类型
     * @return 结果
     */
    public static <T> MultiResponse<T> ofError(String msg) {
        return MultiResBuilder.<T>errorBuilder()
                .msg(msg)
                .build();
    }

    public static <T> MultiResBuilder<T> successBuilder() {
        return new MultiResBuilder<T>()
                .flag(FLAG_SUCCESS)
                .code(ResStatus.success.getCode())
                .msg(ResStatus.success.getMsg());
    }

    public static <T> MultiResBuilder<T> errorBuilder() {
        return new MultiResBuilder<T>()
                .flag(FLAG_FAIL)
                .code(ResStatus.fail.getCode())
                .msg(ResStatus.fail.getMsg());
    }

    public static <T> MultiResBuilder<T> failBuilder() {
        return new MultiResBuilder<T>()
                .flag(FLAG_FAIL)
                .code(ResStatus.fail.getCode())
                .msg(ResStatus.fail.getMsg());
    }


    public static <T> MultiResBuilder<T> warnBuilder() {
        return new MultiResBuilder<T>()
                .flag(FLAG_WARN)
                .code(ResStatus.warn.getCode())
                .msg(ResStatus.warn.getMsg());
    }


    /**
     * 构建出 MultiResponse，根据builder中的内容
     *
     * @return MultiResponse
     */
    public MultiResponse<T> build() {
        return new MultiResponse<>(this);
    }


    /**
     * 返回前台数据
     */
    private Collection<T> data;


    /**
     * 返回前台成功失败标志
     */
    private String flag;

    /**
     * 返回消息
     */
    private String msg;

    /**
     * 返回状态码
     */
    private Integer code;


    /**
     * 返回状态位
     */
    private String status;

    public MultiResBuilder<T> status(String status) {
        this.status = status;
        return this;
    }

    public MultiResBuilder<T> code(Integer code) {
        this.code = code;
        return this;
    }

    public MultiResBuilder<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    public MultiResBuilder<T> flag(String flag) {
        this.flag = flag;
        return this;
    }

    public MultiResBuilder<T> data(Collection<T> data) {
        this.data = data;
        return this;
    }
}
